Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java StringBuffer Class

  • The majority of the functionality of strings is provided by the peer class of StringBuffer. StringBuffer represents expandable and writable character sequences, whereas the string represents fixed-length, immutable character sequences.

  • Characters and substrings may be added to the beginning or end of a StringBuffer. It will automatically expand to accommodate such additions, and frequently preallocates more characters than are actually required to allow for growth.

  • Mutable (modifiable) strings are created with the help of the StringBuffer class. The StringBuffer class in Java is identical to the String class except that it is changeable.






Important Constructors of StringBuffer Class

  • StringBuffer() : StringBuffer() generates a blank String buffer with a 16-character initial capacity.
  • StringBuffer s = new StringBuffer();

  • StringBuffer(String str) : Creates a String buffer with the specified string by calling StringBuffer(String String_variable_name).
  • StringBuffer s = new StringBuffer(10);

  • StringBuffer(int capacity) : StringBuffer(int capacity) creates an empty String buffer with the capacity specified as its length.





  • StringBuffer s = new StringBuffer("DockerTpoint");


Important methods of StringBuffer class


Java String append() Methods


The append() method of the StringBuffer class concatenates the given argument with this string.


 // Java example of String length() Methods
public class Main {	
    public static void main(String args[]){  		 
      StringBuffer sb = new StringBuffer("Exam");
      sb.append("Deva"); //  original string is changed
      System.out.println(sb);
  } 
}



Output:

DockerTpoint 


Java String insert() Method


The insert() method of the StringBuffer class inserts the given string at the given position with this string.


 // Java example of String insert() Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuffer sb = new StringBuffer("Exam");
      sb.insert(4,"Deva"); //  original string is now changed
      System.out.println(sb);
  } 
}



Output:

DockerTpoint 

Java String delete() Method


The delete() method of the StringBuffer class remove the specified string from the begin Index and end Index-1.


 // Java example of String delete  Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuffer sb = new StringBuffer("DockerTpoint"); 
      sb.delete(0,4);  //   original string is now changed
      System.out.println(sb);
  } 
}



Output:

mDeva 

Java String replace Method


The substring method of the StringBuffer class replaces the given string between begin Index and end Index-1.


 // Java example of String substring (int start, int end)  Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuffer sb = new StringBuffer("E  Deva"); 
       sb.replace(1,3,"xam");  // it will replace white space, original string is now changed
      System.out.println(sb);
  } 
}



Output:

DockerTpoint  

Java String reverse Method


The reverse method of the StringBuffer class reverses the current string.


 // Java example of String concat Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuffer sb = new StringBuffer("DockerTpoint"); 
      sb.reverse();
      System.out.println(sb);
  } 
}



Output:

aveDmaxE  

Java String capacity Method


The capacity method of the StringBuffer class returns the buffer's current capacity. The buffer's default capacity is sixteen. If the number of characters exceeds the current capacity, the capacity is increased by (oldcapacity*2)+2.


 // Java example of String indexOf Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuffer sb = new StringBuffer();
      System.out.println(sb.capacity()); // default value  16
      sb.append("DockerTpoint");
      System.out.println(sb.capacity()); // capicity is 16
      sb.append("I Love India");
      System.out.println(sb.capacity()); // Now (16*2)+2=34     i.e (oldcapacity*2)+2  
  } 
}



Output:

16 
16 
34 

Important methods of StringBuffer class

Important methods of StringBuffer class are mention below


Type and Modifier Method Description
public synchronized StringBuffer append(String s) Append(String s) is used to append the specified string to this string. The append() method has several overloads, including append(char), append(boolean), append(int), append(float), append(double)
public synchronized StringBuffer insert(int value, String s) The specific implementation of a method that is already provided by its parent class or superclass is granted by method overriding.
public synchronized StringBuffer replace(int begin Index, int end Index-1, String str) Replace is used to replace the string between the specified begin and end indexes.
public String substring(int begin Index, int end Index) substring returns the substring from the specified beginIndex and endIndex.
public String substring(int beginIndex) Substring returns the substring from the specified begin Index.
public int length() length() returns the length of the string, i.e. the total number of characters.
public char charAt(int index) charAt(int index) returns the character at the specified position.
public int capacity() capacity() returns the current capacity.
public void ensureCapacity(int minimumCapacity) ensureCapacity(int minimumCapacity) ensures that the capacity is at least equal to the specified minimum.
public synchronized StringBuffer reverse() The string can be reversed using reverse().
public synchronized StringBuffer delete(int start Index, int end Index) The string can be removed from the startIndex and endIndex by using the delete() method.
public void setCharAt(int index, char c) In setCharAt(int index, char c) method the character at the specified index is set to c.




Some Interesting StringBuffer Class Facts

  • java.lang.StringBuffer extends (or derives from) the Object class.
  • Serializable, Appendable, and CharSequence are all StringBuffer class interfaces that have been implemented.
  • public final class StringBuffer extends Serializable, CharSequence, and Appendable are all implemented by the object.
  • Multiple threads can use string buffers safely. The methods can be synchronized wherever necessary so that all operations on any given instance behave as if they were performed in some sequential order.
  • This class synchronizes only on the string buffer performing the operation, not on the source, whenever an operation involving a source sequence occurs (such as appending or inserting from a source sequence).
  • StringBuffer inherits some Object class methods such as clone(), equals(), finalize(), getClass(), hashCode(), notifies(), and notifyAll ().
  • StringBuilder, J2SE 5 extends Java's already robust string handling capabilities with a new string class. StringBuilder is the name of this new class. It is identical to StringBuffer with one notable exception: it is not synchronized, which means it is not thread-safe. StringBuilder has the advantage of being faster. However, if you are using multithreading, you must use StringBuffer instead of StringBuilder.

Java StringBuilder Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.